package com.rcp.manager;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



@WebServlet("/InventoryItems")
public class InventoryItems extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		// Define connection variables
		String className = "com.mysql.jdbc.Driver";
		String url		 = "jdbc:mysql://cs3.calstatela.edu:3306/cs437group01";
		String dbusername  = "cs437group01";
		String dbpassword  = "VHj#dj!v";
		ArrayList<String> nameAndQuantity = new ArrayList<String>();

		try {
			// Include the driver class
			Class.forName( className );

			// Establish a connection to the database
			Connection connection =
					DriverManager.getConnection(url, dbusername, dbpassword);

			String query = "SELECT * FROM Ingredients";

			// Get a reference to the statement object that will execute our query
			// on the server
			Statement statement = connection.createStatement();

			// Execute the query and Store the returned records (ResultSet)
			ResultSet resultSet = statement.executeQuery(query);
			String QuantityInStock = "";
			String Name = "";


			while ( resultSet.next() ) 
			{
				Name = resultSet.getString("Name");
				QuantityInStock = resultSet.getString("QuantityInStock");
				nameAndQuantity.add(Name);
				nameAndQuantity.add(QuantityInStock);
			}

			statement.executeQuery(query);	

			// Close our DB Connection
			connection.close();

		}
		catch(SQLException e){		
			e.printStackTrace();
		}
		catch(ClassNotFoundException e){
			e.printStackTrace();
		}

		response.setContentType("text/html");

		PrintWriter out = response.getWriter();

		out.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
		out.println("<head>");
		out.println("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />");
		out.println("<title>Inventory Items</title>");
		out.println("<link rel='stylesheet' type='text/css' href='css/homework1.css' />");
		out.println("<script type='text/javascript' src='js/jquery-1.4.2.js'></script>");
		out.println("<script type='text/javascript' src='js/homework1.js'></script>");
		out.println("</head>");
		out.println("<body>");

		out.println("<div id='header'>");
		out.println("<h1>RCP</h1>");
		out.println("</div>");
		out.println("<p> ");
		out.println("Inventory Items<br />");
		for(int i = 0; i < nameAndQuantity.size();i++)
			{
				if(i % 2 == 0 && i !=0) 
						out.println("<br />");
				out.println(nameAndQuantity.get(i));
			}
		out.println("</p>");
		out.println("</div>");
		out.println("</body>");
		out.println("</html>");
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
